package com.jk.test;

/**
 * Created by dell on 2017/6/14.
 */
public class SaveMoney {
    public static void main(String[] args) {
        Account account = new Account();
        Customer customer = new Customer(account);
        Thread thread1 = new Thread(customer);
        Thread thread2 = new Thread(customer);
        thread1.setName("客户1");
        thread2.setName("客户2");
        thread1.start();
        thread2.start();

    }
}

class Customer implements Runnable {

    private Account account = null;

    public void run() {
        for (int i = 0; i < 3; i++) {
            account.addMoney(1000);
        }
    }

    public Customer() {

    }

    public Customer(Account account) {
        this.account = account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
}

class Account {
    private int m = 0;

    public synchronized void addMoney(int i) {
        m += i;
        try {
            Thread.currentThread().sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"存后" + m);
    }

    public int getMoney() {
        return m;
    }
}